<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2023/12/9
 * Time: 17:13
 */

namespace app\admin\controller;

use app\admin\model\Admin;
use app\BaseController;
use app\admin\validate\LoginValidate;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Response;

class Login extends BaseController
{

    /**
     * 登录 此处对验证码进行了格外的处理1
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function login(): Response
    {
        $param = request()->param();
        validate(LoginValidate::class)->scene("login")->check($param);

        //验证码 实际市场负责人需求 屏蔽验证码
//        if(!env('APP_DEBUG',false)){
//            if (!captcha_check($param['code'], $param['code_id'])) return $this->error("验证码错误");
//        }

        $info = Admin::where('name', $param['name'])->find();
        if (!$info) return $this->error("用户不存在");
        if (md5(request()->param("password").'yan') != $info->password) return $this->error("用户或密码错误");
        $userInfo = $info->toArray();
        unset($userInfo['password']);
        $token = getToken($info->id, 1); // 生成JWT令牌
        return $this->success('成功', compact("userInfo", "token"));
    }


    /**
     * 获取验证码
     * @return Response
     */
    public function captcha(): Response
    {
        $id = uniqid((string)mt_rand(100000, 999999));
        return $this->success('成功', [
            'code'    => request()->domain() . captcha_src($id),
            'code_id' => $id
        ]);
    }



}